/*
    Copyright (C) 2008  Helmut Juskewycz

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package com.juskewycz.dividere;

import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import org.jdesktop.application.Action;

/**
 *
 * @version 
 * @author Helmut Juskewycz (helmut -at- juskewycz.com)
 */
public class FileSplitSizeDialog extends javax.swing.JDialog {
    
    //TODO alles lokalisieren
    //TODO wenn eine action aufgerufen wird, dann kommt ein haesslicher dialog, da muessen noch daten usw. eingesetzt werden
    private final SplitSizeDialogListener listener;
    
    /** Creates new form FileSplitSizeDialog */
    public FileSplitSizeDialog(java.awt.Frame parent, boolean modal, SplitSizeDialogListener listener) {
        super(parent, modal);
        initComponents();
        SpinnerModel model = new SpinnerNumberModel(96, 2, 100, 1);  
        spSplitSize.setModel(model);
        this.listener = listener;
        setTitle("Select split size");
        setLocationRelativeTo(parent);
        //the focus listener does not work directly on the jspinner
        //it has to be attached to the textfield
        //this is more like a bug and not standard behavior
        ((JSpinner.DefaultEditor)spSplitSize.getEditor()).getTextField().addFocusListener(new java.awt.event.FocusAdapter() {
            @Override
            public void focusGained(java.awt.event.FocusEvent evt) {
                sizeButtonGroup.clearSelection();
            }
        });
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        sizeButtonGroup = new javax.swing.ButtonGroup();
        rb2MB = new javax.swing.JRadioButton();
        rb4MB = new javax.swing.JRadioButton();
        rb16MB = new javax.swing.JRadioButton();
        rb32MB = new javax.swing.JRadioButton();
        rb64MB = new javax.swing.JRadioButton();
        spSplitSize = new javax.swing.JSpinner();
        bCancel = new javax.swing.JButton();
        bOk = new javax.swing.JButton();
        lDialogDescription = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        sizeButtonGroup.add(rb2MB);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(com.juskewycz.dividere.DividereApp.class).getContext().getResourceMap(FileSplitSizeDialog.class);
        rb2MB.setText(resourceMap.getString("rb2MB.text")); // NOI18N
        rb2MB.setName("rb2MB"); // NOI18N

        sizeButtonGroup.add(rb4MB);
        rb4MB.setText(resourceMap.getString("rb4MB.text")); // NOI18N
        rb4MB.setName("rb4MB"); // NOI18N

        sizeButtonGroup.add(rb16MB);
        rb16MB.setText(resourceMap.getString("rb16MB.text")); // NOI18N
        rb16MB.setName("rb16MB"); // NOI18N

        sizeButtonGroup.add(rb32MB);
        rb32MB.setText(resourceMap.getString("rb32MB.text")); // NOI18N
        rb32MB.setName("rb32MB"); // NOI18N

        sizeButtonGroup.add(rb64MB);
        rb64MB.setSelected(true);
        rb64MB.setText(resourceMap.getString("rb64MB.text")); // NOI18N
        rb64MB.setName("rb64MB"); // NOI18N

        spSplitSize.setName("spSplitSize"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(com.juskewycz.dividere.DividereApp.class).getContext().getActionMap(FileSplitSizeDialog.class, this);
        bCancel.setAction(actionMap.get("cancel")); // NOI18N
        bCancel.setName("bCancel"); // NOI18N

        bOk.setAction(actionMap.get("ok")); // NOI18N
        bOk.setName("bOk"); // NOI18N

        lDialogDescription.setText(resourceMap.getString("lDialogDescription.text")); // NOI18N
        lDialogDescription.setName("lDialogDescription"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lDialogDescription)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rb2MB)
                    .addComponent(rb4MB)
                    .addComponent(rb16MB))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(spSplitSize)
                    .addComponent(rb32MB, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(rb64MB))
                .addGap(38, 38, 38))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(bCancel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                .addComponent(bOk)
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {bCancel, bOk});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(lDialogDescription)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rb2MB)
                    .addComponent(rb32MB))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rb4MB)
                    .addComponent(rb64MB))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rb16MB)
                    .addComponent(spSplitSize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(44, 44, 44)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bCancel)
                    .addComponent(bOk))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {bCancel, bOk});

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                FileSplitSizeDialog dialog = new FileSplitSizeDialog(new javax.swing.JFrame(), true, null);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    @Action
    public void ok() {
        int oneMB = 1048576;
        long size;
        if(rb2MB.isSelected()) {
            size = oneMB*2;
        } else if(rb4MB.isSelected()) {
            size = oneMB*4;
        } else if(rb16MB.isSelected()) {
            size = oneMB*16;
        } else if(rb32MB.isSelected()) {
            size = oneMB*32;
        } else if(rb64MB.isSelected()) {
            size = oneMB*64;
        } else {
            size = oneMB*((Integer)spSplitSize.getValue());
        }
        listener.splitSize = size;
        setVisible(false);
    }

    @Action
    public void cancel() {
        listener.splitSize = -1;
        setVisible(false);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bCancel;
    private javax.swing.JButton bOk;
    private javax.swing.JLabel lDialogDescription;
    private javax.swing.JRadioButton rb16MB;
    private javax.swing.JRadioButton rb2MB;
    private javax.swing.JRadioButton rb32MB;
    private javax.swing.JRadioButton rb4MB;
    private javax.swing.JRadioButton rb64MB;
    private javax.swing.ButtonGroup sizeButtonGroup;
    private javax.swing.JSpinner spSplitSize;
    // End of variables declaration//GEN-END:variables
    
}
